<?php
/**
 * [exsampleController]
 *
 * フレームワークの使い方サンプルコントローラ
 *
 * PHP version 5
 *
 * @package     light-php
 * @author      kamisiro.memo <kamisiro.memo@gmail.com>
 */
class exsampleController extends appController
{
    /**
     * [uses] 
     * 
     * @var array
     * @access public
     */
    var $uses = array(
            'ExsampleUser' 
        );

    /**
     * [execute]
     *
     * @return none
     * @access public
     */
    public function execute()
    {
        $req = $this->getRequestParameters();
        
        //モード振り分け
        switch ($req['mode']) {
        case 'upd':
            $this->updateUserList($req);
            break;
        }

        $user_list = $this->ExsampleUser->findList(null, 'id asc');
        $this->set('user_list', $user_list);
    }

    /**
     * [updateUserList] 
     * 
     * @param mixed $req 
     * @access public
     * @return void
     */
    public function updateUserList($req) {
        
        if (!isset($req['upd_id'])) {
            return false; 
        }
        foreach ($req['upd_id'] as $id => $value) {

            //削除処理
            if (isset($req['upd_del'][$id])) {
                $this->ExsampleUser->deleteOne(array('id'=>$id));
                continue; 
            }
            

            $upd_params = array(
                'name'  => isset($req['upd_name'][$id]) ? $req['upd_name'][$id] : '',
                'mail'  => isset($req['upd_mail'][$id]) ? $req['upd_mail'][$id] : '',
            );

            if ($id === 'new') {
                if ($upd_params['name'] != '' && $upd_params['mail'] != '') {
                    $upd_params['password'] = 'hogehoge';
                    $this->ExsampleUser->saveOne($upd_params);
                }
            } else {
                if ($upd_params['name'] != '' && $upd_params['mail'] != '') {
                    $this->ExsampleUser->updateOne($upd_params, array('id'=>$id));
                }
            }
        }
    }
}
